package controllers

import (
	"encoding/json"
	"time"
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
)

type GspController struct {
	BaseController
}

//采购记录
func (c *GspController) Entry() {
	c.checkAuthor("gsp/entry")

	if c.Ctx.Input.IsPost() { //POST提交返回数据

		var params models.ErpEntryDataQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Depid = c.curUser.DepartmentId
		params.Typeid = []int{20, 50, 70}
		params.Auditing = 4 //获取已入库的
		data, total := models.ErpEntryDataPageList(&params)

		c.jsonDataTable(total, data)
	}

	suppliers := models.ErpSuppliersList(&models.ErpSuppliersQueryParam{DepartmentId: c.curUser.DepartmentId})
	c.Data["suppliers"] = suppliers

	c.Data["activeSidebarUrl"] = "/gsp/entry"
	c.display("", "gsp/entry_footerjs.html")
}

//验收记录
func (c *GspController) Acceptance() {
	c.checkAuthor("gsp/acceptance")

	if c.Ctx.Input.IsPost() { //POST提交返回数据

		var params models.ErpAcceptanceDataQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Depid = c.curUser.DepartmentId
		params.Typeid = []int{20, 50, 70}
		params.RelatedSel = []interface{}{"Products", "Acceptance", "Products__Suppliers","Snd"}
		data, total := models.ErpAcceptanceDataPageList(&params)

		c.jsonDataTable(total, data)
	}

	suppliers := models.ErpSuppliersList(&models.ErpSuppliersQueryParam{DepartmentId: c.curUser.DepartmentId})
	c.Data["suppliers"] = suppliers

	c.Data["activeSidebarUrl"] = "/gsp/acceptance"
	c.display("", "gsp/acceptance_footerjs.html")
}

//入库记录
func (c *GspController) Warehousing() {
	c.checkAuthor("gsp/warehousing")

	if c.Ctx.Input.IsPost() { //POST提交返回数据

		var params models.ErpFlowingDataQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Department = c.curUser.DepartmentId
		params.PTypeid = []int{20, 50, 70}
		params.Typeidn = "1"
		params.RelatedSel = []interface{}{"Products", "Acceptance", "Flowing", "Products__Suppliers","Snd"}
		data, total := models.ErpFlowingDataPageList(&params)

		c.jsonDataTable(total, data)
	}

	suppliers := models.ErpSuppliersList(&models.ErpSuppliersQueryParam{DepartmentId: c.curUser.DepartmentId})
	c.Data["suppliers"] = suppliers

	c.Data["activeSidebarUrl"] = "/gsp/warehousing"
	c.display("", "gsp/warehousing_footerjs.html")
}

//采退记录
func (c *GspController) Entryreturn() {
	c.checkAuthor("gsp/entryreturn")

	if c.Ctx.Input.IsPost() { //POST提交返回数据

		var params models.ErpFlowingDataQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Department = c.curUser.DepartmentId
		params.PTypeid = []int{20, 50, 70}
		params.Typeidn = "3"
		params.RelatedSel = []interface{}{"Products", "Acceptance", "Products__Suppliers","Snd"}
		data, total := models.ErpFlowingDataPageList(&params)

		c.jsonDataTable(total, data)
	}

	suppliers := models.ErpSuppliersList(&models.ErpSuppliersQueryParam{DepartmentId: c.curUser.DepartmentId})
	c.Data["suppliers"] = suppliers

	c.Data["activeSidebarUrl"] = "/gsp/entryreturn"
	c.display("", "gsp/entryreturn_footerjs.html")
}

//养护计划清单
func (c *GspController) CuringPlan() {
	c.checkAuthor("gsp/curing_plan")

	if c.Ctx.Input.IsPost() { //POST提交返回数据

		var params models.ErpCuringDataQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Status = []int{2}
		params.Depid = c.curUser.DepartmentId
		data, total := models.ErpCuringDataPageList(&params)

		c.jsonDataTable(total, data)
	}

	c.Data["activeSidebarUrl"] = "/gsp/curing_plan"
	c.display("", "gsp/curingplan_footerjs.html")
}

//养护计划检查清单
func (c *GspController) CuringCheck() {
	c.checkAuthor("gsp/curing_check")

	if c.Ctx.Input.IsPost() { //POST提交返回数据

		var params models.ErpCuringDataQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Status = []int{2}
		params.Depid = c.curUser.DepartmentId
		data, total := models.ErpCuringDataPageList(&params)

		c.jsonDataTable(total, data)
	}

	c.Data["activeSidebarUrl"] = "/gsp/curing_check"
	c.display("", "gsp/curingcheck_footerjs.html")
}

//近效催销表
func (c *GspController) Neareffect() {
	c.checkAuthor("gsp/neareffect")
	if c.Ctx.Input.IsPost() { //POST提交返回数据
		var params models.ErpInventoryQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Department = c.curUser.DepartmentId
		if params.Typeid == 0 {
			params.Typeids = []int{20, 50, 70}
		}
		params.Isnegative = "1" //有库存
		params.Sort = "expiry_date"
		data, total := models.ErpInventoryPageList(&params)

		c.jsonDataTable(total, data)
	}
	c.Data["activeSidebarUrl"] = "/gsp/neareffect"
	c.display("", "gsp/neareffect_footerjs.html")
}

//器械质量档案维护
func (c *GspController) Quality() {
	c.checkAuthor("gsp/quality")
	if c.Ctx.Input.IsPost() { //POST提交返回数据
		var params models.GsparchivesQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.DepartmentId = c.curUser.DepartmentId
		data, total := models.GsparchivesPageList(&params)
		c.jsonDataTable(total, data)
	}
	c.Data["activeSidebarUrl"] = "/gsp/quality"
	c.display("", "gsp/quality_footerjs.html")
}

//获取指定类型产品
func (c *GspController) ProductsDataGrid() {
	var params models.ErpProductsQueryParam
	json.Unmarshal(c.Ctx.Input.RequestBody, &params)
	params.DepartmentId = c.curUser.DepartmentId
	params.Typeids = []int{20, 50, 70}
	params.NoSetId = models.GetExistProductId(c.curUser.DepartmentId)
	params.Status = 1
	data, total := models.ErpProductsPageList(&params)
	c.jsonDataTable(total, data)
}

//新增质量档案
func (c *GspController) QualityInsert() {
	c.checkAuthor("gsp/quality")

	if c.Ctx.Input.IsPost() {
		base := new(models.Gsparchives)
		if err := c.ParseForm(base); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		var select_id []int64
		c.Ctx.Input.Bind(&select_id, "select_id")
		base.CreatedAt = time.Now().Unix()
		base.CreatedBy = c.curUser.RealName
		base.DepartmentId = c.curUser.DepartmentId
		if err := models.GsparchivesAdd(base, select_id); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		c.jsonResult(enums.JRCodeSucc, "ok", "")
	}

	c.display("", "gsp/qualityinsert_footerjs.html", "shared/layout_dialog.html")
}

//修改质量档案
func (c *GspController) QualityUpdate() {
	c.checkAuthor("gsp/quality")
	id, _ := c.GetInt64(":id")
	if id == 0 {
		c.pageError("参数错误")
	}
	gsparchives, err := models.GetGsparchivesById(id, c.curUser.DepartmentId)
	if err != nil || gsparchives.Id == 0 {
		c.pageError("参数错误")
	}

	if c.Ctx.Input.IsPost() {
		if err := c.ParseForm(gsparchives); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		var select_id []int64
		c.Ctx.Input.Bind(&select_id, "select_id")
		gsparchives.UpdatedAt = time.Now().Unix()
		gsparchives.UpdatedBy = c.curUser.RealName
		if err := models.GsparchivesEdit(gsparchives); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		c.jsonResult(enums.JRCodeSucc, "ok", "")
	}

	c.Data["m"] = gsparchives

	c.display("", "gsp/qualityupdate_footerjs.html", "shared/layout_dialog.html")
}
